package com.cast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cast.dao.CheckItemDao;
import com.cast.entity.PageResult;
import com.cast.entity.QueryPageBean;
import com.cast.pojo.CheckItem;
import com.cast.service.CheckItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> checkItems = checkItemDao.findByPage(queryString);
        long total = checkItems.getTotal();
        List<CheckItem> rows = checkItems.getResult();
        PageResult pageResult = new PageResult(total, rows);
        return pageResult;
    }

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemDao.addCheckItem(checkItem);
    }

    @Override
    public CheckItem findById(int id) {
        CheckItem item = checkItemDao.findById(id);
        return item;
    }

    @Override
    public void deleteById(int id) {
//        先查询关联表
        int count = checkItemDao.findFromMiddle(id);
        System.out.println(count);
     /*   if (count >0){
//            或者先删除中间表再删除

          throw   new RuntimeException();
        }*/
        checkItemDao.deleteById(id);
    }

    @Override
    public void updateCheckItem(CheckItem checkItem) {
        checkItemDao.updateCheckItem(checkItem);
    }


}
